#include<iostream>
#include<string>
#include<vector>
#include<tuple>

bool parsePattern()
{
    std::string _pattern = "sg{}fsg%d{%Y-%m-%d %H:%M:%S}%Tsdf%t%T[%p]%T[%c]%T%f:%l%T%m%n";
    std::cout << _pattern << std::endl; 

    std::vector<std::tuple<std::string, std::string, int>> arry;
    std::string format_key;
    std::string format_val;
    std::string string_row;
    bool error_flag = false;
    int pos = 0;
    while(pos < _pattern.size()) 
    {
        if(_pattern[pos] != '%')
        {
            string_row.append(1, _pattern[pos + 1]);
            continue; 
        }

        if(pos + 1 < _pattern.size() && _pattern[pos + 1] == '%')
        {
            string_row.append(1, '%');
            pos += 2;
            continue; 
        }

        if(string_row.empty() == false) 
        {
            std::cout << "[" << string_row << "]<-->[" << "]" << 0 << std::endl; 
            arry.push_back(std::make_tuple(string_row, "", 0));
            string_row.clear(); 
        }

        pos += 1;
        if(pos < _pattern.size() && isalpha(_pattern[pos])) 
        {
            format_key = _pattern[pos];
        }

        else 
        {
            std::cout << &_pattern[pos - 1] << "位置附近格式错误!\n"; 
            return false; 
        }

        pos += 1;
        if(pos < _pattern.size() && _pattern[pos] == '{') 
        {
            error_flag = true; 
            pos += 1;
            while(pos < _pattern.size()) 
            {
                if(_pattern[pos] == '}') 
                {
                    error_flag = false;
                    pos += 1;
                    break; 
                }
                format_val.append(1, _pattern[pos++]); 
            }
        }

        std::cout << "[" << format_key << "]<-->{<F12>[}" << format_val << "]1" << std::endl; 
        arry.push_back(std::make_tuple(format_key, format_val, 1)); 
        format_key.clear();
        format_val.clear(); 
    }

    if(string_row.empty() == false) 
        arry.push_back(std::make_tuple(string_row, "", 0));
    if(format_key.empty() == false) 
        arry.push_back(std::make_tuple(format_key, format_val, 1)); 
    if(error_flag == true)
    {
        std::cout << "子格式花括号匹配错误!\n";
        return false; 
    }
}

int main()
{
    parsePattern();

    return 0;
}
